package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AttachFile;
import com.ruoyi.system.mapper.AttachFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GoodsMapper;
import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.service.IGoodsService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 茶叶商品Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-20
 */
@Service
public class GoodsServiceImpl implements IGoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private AttachFileMapper attachFileMapper;

    /**
     * 查询茶叶商品
     *
     * @param id 茶叶商品主键
     * @return 茶叶商品
     */
    @Override
    public Goods selectGoodsById(Long id) {
        Goods goods = goodsMapper.selectGoodsById(id);
        AttachFile attachFile = new AttachFile();
        attachFile.setSourceId(id);
        attachFile.setFileClass("01");
        List<AttachFile> attachFiles = attachFileMapper.selectAttachFileList(attachFile);
        if (attachFiles.size() != 0) {
            goods.setAttachFiles(attachFiles.get(0));
        }
        attachFile.setFileClass("03");
        List<AttachFile> detailsfileLists = attachFileMapper.selectAttachFileList(attachFile);
        goods.setDetailsfileList(detailsfileLists);
        return goods;
    }

    /**
     * 查询茶叶商品列表
     *
     * @param goods 茶叶商品
     * @return 茶叶商品
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods) {
        return goodsMapper.selectGoodsList(goods);
    }

    /**
     * 新增茶叶商品
     *
     * @param goods 茶叶商品
     * @return 结果
     */
    @Override
    @Transactional
    public int insertGoods(Goods goods) {
        goods.setCreateTime(DateUtils.getNowDate());
        int i = goodsMapper.insertGoods(goods);
        // 商品只有一个主图 已在前端控制
        if (goods.getAttachFiles() != null) {
            goods.getAttachFiles().setSourceId(goods.getId());
            goods.getAttachFiles().setCreateTime(DateUtils.getNowDate());
            attachFileMapper.insertAttachFile(goods.getAttachFiles());
        }
        if (goods.getDetailsfileList() != null) {
            int num = 0;
            for (int x = 0; x < goods.getDetailsfileList().size(); x++) {
                goods.getDetailsfileList().get(x).setSort(++num);
                goods.getDetailsfileList().get(x).setSourceId(goods.getId());
                goods.getDetailsfileList().get(x).setCreateTime(DateUtils.getNowDate());
                attachFileMapper.insertAttachFile(goods.getDetailsfileList().get(x));
            }
        }
        return i;
    }

    /**
     * 修改茶叶商品
     *
     * @param goods 茶叶商品
     * @return 结果
     */
    @Override
    @Transactional
    public int updateGoods(Goods goods) {
        goods.setUpdateTime(DateUtils.getNowDate());

        if (goods.getAttachFiles() != null) {
            if (goods.getAttachFiles().getId() != null) {
                goods.getAttachFiles().setUpdateTime(DateUtils.getNowDate());
                attachFileMapper.updateAttachFile(goods.getAttachFiles());
            } else {
                goods.getAttachFiles().setSourceId(goods.getId());
                goods.getAttachFiles().setCreateTime(DateUtils.getNowDate());
                attachFileMapper.insertAttachFile(goods.getAttachFiles());
            }
        }

        if (goods.getDetailsfileList() != null) {
            goods.getDetailsfileList().forEach(bean -> {
                if (bean.getId() != null) {
                    bean.setUpdateTime(DateUtils.getNowDate());
                    attachFileMapper.updateAttachFile(bean);
                } else {
                    bean.setSourceId(goods.getId());
                    bean.setCreateTime(DateUtils.getNowDate());
                    attachFileMapper.insertAttachFile(bean);
                }
            });
        }

        if (goods.getDelAttachFilesIds().length != 0) {
            attachFileMapper.deleteAttachFileByIds(goods.getDelAttachFilesIds());
        }
        return goodsMapper.updateGoods(goods);

    }

    /**
     * 批量删除茶叶商品
     *
     * @param ids 需要删除的茶叶商品主键
     * @return 结果
     */
    @Override
    public int deleteGoodsByIds(Long[] ids) {
        return goodsMapper.deleteGoodsByIds(ids);
    }

    /**
     * 删除茶叶商品信息
     *
     * @param id 茶叶商品主键
     * @return 结果
     */
    @Override
    public int deleteGoodsById(Long id) {
        return goodsMapper.deleteGoodsById(id);
    }
}
